/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2019
 */

package com.jzwl.site.wap.articlestudy.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.constant.Constants;
import com.jzwl.common.id.Sequence;
import com.jzwl.system.base.dao.BaseDAO;

@Repository("siteArticleUserStudyDao")
public class SiteArticleUserStudyDao {

	@Autowired
	private Constants constants;

	@Autowired
	private BaseDAO baseDAO;//dao基类，操作数据库

	public boolean addSiteArticleUserStudy(Map<String, Object> map) {

		//自动注入时间戳为ID 酌情修改数据库类型为bigint  int会越界
		map.put("id", Sequence.nextId());
		map.put("createDate", new Date());
		map.put("isDelete", 0);
		String sql = "insert into site_article_user_study " + " (id,openId,articleId,createDate,isDelete) "
				+ " values " + " (:id,:openId,:articleId,:createDate,:isDelete)";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public String getColumns() {
		return "" + " id as id," + " openId as openId," + " articleId as articleId," + " createDate as createDate,"
				+ " isDelete as isDelete";
	}

	public List querySiteArticleUserStudyList(Map<String, Object> map) {

		String sql = "select wu.`name`,wu.headImgUrl from " + constants.getCurrentDB()
				+ "site_article_user_study t LEFT JOIN " + constants.getCurrentDB()
				+ "wx_user wu on t.openId = wu.openId where t.isDelete = 0";

		if (null != map.get("articleId") && StringUtils.isNotEmpty(map.get("articleId").toString())) {
			sql = sql + " and t.articleId  = " + map.get("articleId") + "";
		}

		sql = sql + " order by t.id ";

		return baseDAO.queryForList(sql);

	}

	public List querySiteArticleUserStudy(Map<String, Object> map) {

		String sql = "select t.* from " + constants.getCurrentDB() + "site_article_user_study t LEFT JOIN "
				+ constants.getCurrentDB()
				+ "wx_user wu on t.openId = wu.openId where t.isDelete = 0 and t.articleId  = " + map.get("articleId")
				+ "and t.openId='" + map.get("articleId") + "'";

		return baseDAO.queryForList(sql);

	}

	public boolean updateSiteArticleUserStudy(Map<String, Object> map) {

		String sql = "update v2_site_article_user_study set "
				+ " id=:id,openId=:openId,articleId=:articleId,createDate=:createDate,isDelete=:isDelete "
				+ " where id=:id";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean deleteSiteArticleUserStudy(Map<String, Object> map) {

		String sql = "delete from v2_site_article_user_study where id in (" + map.get("id") + " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> getById(Map<String, Object> map) {

		Map<String, Object> resMap = new HashMap<String, Object>();

		String sql = "select " + getColumns() + " from v2_site_article_user_study where id = " + map.get("id") + "";

		resMap = baseDAO.queryForMap(sql);

		return resMap;

	}

}
